package parser.nodes;

import codegen.AbstractDescr;
import codegen.CodeGen;
import codegen.RecordDescr;
import codegen.VarDescr;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import scanner.Token;

// RecordType = 'RECORD' FieldList {';' FieldList} 'END' /*FieldList kann leer sein*/
public class RecordTypeNode extends Node {
    private List<FieldListNode> fldLists = new ArrayList<>();
    
    public RecordTypeNode(FieldListNode fieldList) {
        fldLists.add(fieldList);
    }

    public RecordTypeNode() {}
    
    public void addFieldList(FieldListNode fieldList) {
        fldLists.add(fieldList);
    }
    
    @Override
    public void print() {
        p("RecordType:");
        indent();
        p("RECORD");
        for(FieldListNode fldList : fldLists) {
            fldList.print();
            p(";");
        }
        p("END");
        unindent();
    }

    @Override
    public void abstractPrint() {
        p("RECORDTYPE");
        indent();
        for(FieldListNode fldList : fldLists) {
            fldList.abstractPrint();
        }
        unindent();
    }

    @Override
    public AbstractDescr getDescriptor(CodeGen c) throws CompileError {
        c.levelUp();
        for(FieldListNode fieldList : fldLists)
            for(Token identifier : fieldList.getList()) 
                c.putDescr(identifier.toString(), new VarDescr(c.nextFreeAddress(), fieldList.getType().getDescriptor(c), c.getLevel()));
        
        return new RecordDescr(c.nextFreeAddress(),c.levelDown());
    }
}
